package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.DistrictMapper;
import com.hx.hospital.pojo.District;
import com.hx.hospital.service.DistrictService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class DistrictServiceImpl implements DistrictService {

    @Autowired
    private DistrictMapper districtMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<District> findAll() {
        return districtMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<District> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<District> districts = (Page<District>) districtMapper.selectAll();
        return new PageResult<District>(districts.getTotal(),districts.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<District> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return districtMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<District> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<District> districts = (Page<District>) districtMapper.selectByExample(example);
        return new PageResult<District>(districts.getTotal(),districts.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public District findById(Long id) {
        return districtMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param district
     */
    public void add(District district) {
        districtMapper.insert(district);
    }

    /**
     * 修改
     * @param district
     */
    public void update(District district) {
        districtMapper.updateByPrimaryKeySelective(district);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        districtMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(District.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 院区名
            if(searchMap.get("hospitalName")!=null && !"".equals(searchMap.get("hospitalName"))){
                criteria.andLike("hospitalName","%"+searchMap.get("hospitalName")+"%");
            }
            // 院区地址
            if(searchMap.get("hospitalAddress")!=null && !"".equals(searchMap.get("hospitalAddress"))){
                criteria.andLike("hospitalAddress","%"+searchMap.get("hospitalAddress")+"%");
            }
            // 图片
            if(searchMap.get("hospitalImage")!=null && !"".equals(searchMap.get("hospitalImage"))){
                criteria.andLike("hospitalImage","%"+searchMap.get("hospitalImage")+"%");
            }


        }
        return example;
    }

}
